package edu.xhu.communityantiepidemicsystemback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Community;
import edu.xhu.communityantiepidemicsystemback.entity.Material;
import edu.xhu.communityantiepidemicsystemback.mapper.MaterialMapper;
import edu.xhu.communityantiepidemicsystemback.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Autowired
    private  MaterialMapper  materialMapper;

    @Override
    public IPage<Material> getAllMaterialInfo(Query query) {
        Page<Material> page=new Page<>();
        page.setCurrent(query.getPageNO());
        page.setSize(query.getPageSize());
        return this.page(page);
    }

    @Override
    public IPage<Material> fuzzyMaterialInfo(Fuzzy fuzzy) {
        Page<Material> page=new Page<>();
        page.setCurrent(fuzzy.getPageNO());
        page.setSize(fuzzy.getPageSize());
        QueryWrapper<Material> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(fuzzy.getSelect(),fuzzy.getSearchStr());
        return this.page(page,queryWrapper);
    }


    @Override
    public boolean addMaterial(CreatMaterial creatMaterial) {
        Material material=new Material();
        BeanUtils.copyProperties(creatMaterial,material);
        material.setCreateTime(LocalDateTime.now());
        material.setCreator("system");
        return this.save(material);
    }

    @Override
    public boolean delMaterial(Integer id) {
        return this.removeById(id);
}

    @Override
    public boolean updateMaterial(UpdateMaterial updateMaterial) {
        Material material=new Material();
        BeanUtils.copyProperties(updateMaterial,material);
        material.setUpdateTime(LocalDateTime.now());
        material.setOperator("system");
        return this.updateById(material);
    }
}
